home *** CD-ROM | disk | FTP | other *** search
/ Pocket PC Game Programming / Pocket PC Game Programming.iso / source.exe / CH15 / Project01 / Project01.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-01  |  8.9 KB  |  298 lines

  1. //////////////////////////////////////////////////////////////////////
  2. // Pocket PC Game Programming
  3. // Chapter 15: Infrared and Socket Communications
  4. //  
  5. // Project01.cpp - Infrared Test
  6. //
  7. // 
  8. //////////////////////////////////////////////////////////////////////
  9.  
  10. #include "stdafx.h"
  11. #include "Project01.h"
  12.  
  13. //////////////////////////////////////////////////////////////////////
  14. // GameInit
  15. // Triggered by WinMain before window is created
  16. //////////////////////////////////////////////////////////////////////
  17. BOOL GameInit(HINSTANCE hInst)
  18. {
  19.     //create new game library objecti
  20.     Game = new CGameLibrary(hInst, _T("Project01"));
  21.     if (Game == NULL)
  22.         return FALSE;
  23.  
  24.     //set up the game window for creation
  25.     Game->SetFullscreen(TRUE);
  26.     Game->G_SetDisplay(FALSE);
  27.     Game->SetTitle(_T("Infrared Test"));
  28.     Game->SetFrameRate(FRAME_RATE);
  29.  
  30.     return TRUE;
  31. }
  32.  
  33. //////////////////////////////////////////////////////////////////////
  34. // GameStart
  35. // Triggered by WndProc after game window is activated
  36. //////////////////////////////////////////////////////////////////////
  37. void GameStart(HWND hWnd)
  38. {
  39.     HDC hdc;
  40.  
  41.     // seed the random number generator
  42.     srand(GetTickCount());
  43.  
  44.     //create double buffer
  45.     cbDoubleBuffer = new CBitmap(GetDC(hWnd));
  46.     if (!cbDoubleBuffer->Create(Game->ScreenWidth(), Game->ScreenHeight()))
  47.         Game->FatalError(_T("Error creating double buffer"));
  48.  
  49.     //use the double buffer
  50.     hdc = cbDoubleBuffer->GetSourceDC();
  51.  
  52.     //load background image
  53.     cbBackground = new CBitmap(hdc);
  54.     if (!cbBackground->Load(Game->GetPath(L"infrared.bmp")))
  55.         Game->FatalError(_T("Error loading infrared.bmp"));
  56.  
  57.     //draw background image to double buffer
  58.     cbBackground->BitBlit(0, 0);
  59.  
  60. }
  61.  
  62. //////////////////////////////////////////////////////////////////////
  63. // GameActivate
  64. // Triggered by WndProc when game is activated
  65. //////////////////////////////////////////////////////////////////////
  66. void GameActivate(HWND hWnd)
  67. {
  68. }
  69.  
  70. //////////////////////////////////////////////////////////////////////
  71. // GamePaint
  72. // Window repaint event (usually triggered once at startup)
  73. //////////////////////////////////////////////////////////////////////
  74. void GamePaint(HWND hWnd)
  75. {
  76.     HDC hdc;
  77.     PAINTSTRUCT ps;
  78.     
  79.     hdc = BeginPaint(hWnd, &ps);
  80.  
  81.     //draw double buffer to screen
  82.     BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(), 
  83.         cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
  84.  
  85.     EndPaint(hWnd, &ps);
  86. }
  87.  
  88. //////////////////////////////////////////////////////////////////////
  89. // GameEvent
  90. // Triggered by the message loop in WinMain
  91. //////////////////////////////////////////////////////////////////////
  92. void GameEvent()
  93. {
  94.     HWND hWnd;
  95.     HDC hdc;
  96.  
  97.     hWnd = Game->GetWindow();
  98.     hdc = GetDC(hWnd);
  99.  
  100.     //draw background image to double buffer
  101.     cbBackground->BitBlit(0, 0);
  102.  
  103.     CheckInfrared(cbDoubleBuffer->GetSourceDC());
  104.  
  105.     //draw double buffer to screen
  106.     BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(), 
  107.         cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
  108.  
  109.     //never delete the main DC, only release it!
  110.     ReleaseDC(hWnd, hdc);
  111.  
  112. }
  113.  
  114. //////////////////////////////////////////////////////////////////////
  115. // CheckInfrared
  116. // 
  117. //////////////////////////////////////////////////////////////////////
  118. void CheckInfrared(HDC hdc)
  119. {
  120.     SOCKET irSocket;
  121.     char chBuffer[1024];
  122.     char szHostName[1024];
  123.     HOSTENT *lphostent;
  124.     in_addr address;
  125.     int nSize;
  126.     int sockRet;
  127.     TCHAR szTemp[30];
  128.     DEVICELIST *pDevList;
  129.     int line = 5, xPos = 4;
  130.     WSADATA wsaData;
  131.     
  132.  
  133.     //check the winsock library
  134.     sockRet = WSAStartup(MAKEWORD(1,1), &wsaData);
  135.     if (sockRet != 0)
  136.     {
  137.         Game->PrintText(hdc, _T("Winsock library not found!"), xPos, line, RGB(255,255,255), TRANSPARENT);
  138.         return;
  139.     }
  140.  
  141.     //winsock will return version requested if valid
  142.     if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
  143.     {
  144.         Game->PrintText(hdc, _T("Winsock library version is invalid!"), xPos, line, RGB(255,255,255), TRANSPARENT);
  145.         WSACleanup();
  146.         return;
  147.     }
  148.  
  149.     //display winsock version
  150.     wsprintf(szMessage, _T("Winsock version: %i.%i"), 
  151.         HIBYTE(wsaData.wHighVersion), LOBYTE(wsaData.wHighVersion));
  152.     Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  153.     
  154.     //display max sockets
  155.     wsprintf(szMessage, _T("Maximum sockets: %i"), wsaData.iMaxSockets);
  156.     Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  157.  
  158.     //display max packet size
  159.     //if limit is 0, use implicit limit of 8192
  160.     sockRet = wsaData.iMaxUdpDg;
  161.     if (sockRet == 0) 
  162.         sockRet = 8192;
  163.     wsprintf(szMessage, _T("Maximum packet size: %i"), sockRet);
  164.     Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  165.  
  166.     //display buffer size
  167.     nSize = sizeof(chBuffer);
  168.     wsprintf(szMessage, _T("Socket buffer size: %i"), nSize);
  169.     Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  170.  
  171.     //find ip address
  172.     sockRet = gethostname(szHostName, 1024);
  173.     if (sockRet == SOCKET_ERROR)
  174.     {
  175.         Game->PrintText(hdc, _T("Could not find host name"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  176.         WSACleanup();
  177.         return;
  178.     }
  179.  
  180.     //display host name
  181.     mbstowcs(szTemp, szHostName ,1024);
  182.     wsprintf(szMessage, _T("Local host name: %s"), szTemp);
  183.     Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  184.  
  185.     //display ip address
  186.     lphostent = gethostbyname(szHostName);
  187.     if (lphostent == NULL)
  188.     {
  189.         Game->PrintText(hdc, _T("Error finding ip address"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  190.         WSACleanup();
  191.         return;
  192.     }
  193.  
  194.     for (int i = 0; lphostent->h_addr_list[i] != NULL; i++)
  195.     {
  196.         memcpy(&address, lphostent->h_addr_list[i], sizeof(address));
  197.         wsprintf(szMessage, _T("Local host ip: %i.%i.%i.%i"), 
  198.             address.S_un.S_un_b.s_b1,
  199.             address.S_un.S_un_b.s_b2,
  200.             address.S_un.S_un_b.s_b3,
  201.             address.S_un.S_un_b.s_b4);
  202.         Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  203.     }
  204.     
  205.     //open the socket port
  206.     irSocket = socket(AF_IRDA, SOCK_STREAM, 0);
  207.     if (irSocket == INVALID_SOCKET)
  208.     {
  209.         Game->PrintText(hdc, _T("Error opening infrared port!"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  210.         WSACleanup();
  211.         return;
  212.     }
  213.  
  214.     line++;
  215.     Game->PrintText(hdc, _T("Infrared socket opened"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  216.  
  217.     sockRet = getsockopt(irSocket, SOL_IRLMP, IRLMP_ENUMDEVICES, chBuffer, &nSize);
  218.     if (sockRet == SOCKET_ERROR)
  219.     {
  220.         Game->PrintText(hdc, _T("Error receiving device list"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  221.         closesocket(irSocket);
  222.         WSACleanup();
  223.         return;
  224.     }
  225.  
  226.     Game->PrintText(hdc, _T("Received device list"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  227.  
  228.     pDevList = (DEVICELIST*) chBuffer;
  229.     if (pDevList->numDevice > 0)
  230.     {
  231.         Game->PrintText(hdc, _T("Infrared device found!"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  232.         mbstowcs(szTemp, pDevList->Device[0].irdaDeviceName, 
  233.             sizeof(pDevList->Device[0].irdaDeviceName));
  234.         wsprintf(szMessage, _T("Remote device name: %s"), szTemp);
  235.         Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
  236.     }
  237.     else
  238.     {
  239.         Game->PrintText(hdc, _T("No infrared activity"), xPos, line++, RGB(255,255,255), TRANSPARENT);
  240.     }
  241.  
  242.     closesocket(irSocket);
  243.  
  244.     WSACleanup();
  245. }
  246.  
  247. //////////////////////////////////////////////////////////////////////
  248. // StylusDown
  249. // Stylus press event
  250. //////////////////////////////////////////////////////////////////////
  251. void StylusDown(int x, int y)
  252. {
  253.     //tap the screen to end program 
  254.     if (x > 45 && x < 195 && y > 275)
  255.         Game->Shutdown();
  256. }
  257.  
  258. //////////////////////////////////////////////////////////////////////
  259. // GameEnd
  260. // End of program event
  261. //////////////////////////////////////////////////////////////////////
  262. void GameEnd() 
  263. {
  264.     HWND hWnd = Game->GetWindow();
  265.     HDC hdc = GetDC(hWnd);
  266.  
  267.     delete Game;
  268.     delete cbDoubleBuffer;
  269.     delete cbBackground;
  270.     DeleteDC(hdc);
  271.  
  272. }
  273.  
  274. //////////////////////////////////////////////////////////////////////
  275. // StylusMove
  276. // Stylus drag across screen event
  277. //////////////////////////////////////////////////////////////////////
  278. void StylusMove(int x, int y)
  279. {
  280. }
  281.  
  282. //////////////////////////////////////////////////////////////////////
  283. // StylusUp
  284. // Stylus release event
  285. //////////////////////////////////////////////////////////////////////
  286. void StylusUp(int x, int y)
  287. {
  288. }
  289.  
  290. void ButtonPress(int iButtonID, POINT pt)
  291. {
  292. }
  293.  
  294. void ButtonRelease(int iButtonID, POINT pt)
  295. {
  296. }
  297.  
  298.